﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Blog.Framework.Utility
{
    public static class Singleton
    {
        public static T GetInstance<T>(string classname)
        {
            return GetInstance<T>(classname, null);
        }

        public static T GetInstance<T>(string classname, string key)
        {
            if (HttpContext.Current == null)
            {
                return Reflection.CreateClass<T>(classname);
            }

            if (key == null)
            {
                key = HttpContext.Current.GetHashCode().ToString("x") + ":" + classname;
            }

            T ret = (T)HttpContext.Current.Items[key];
            if (ret == null)
            {
                ret = Reflection.CreateClass<T>(classname);
                HttpContext.Current.Items[key] = ret;
            }

            return ret;
        }
    }
}
